package com.agent.ai.feign;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Service
public class AIFeignService {

    @Autowired
    private AIFeignClient feignClient;

    /**
     * 获取流程节点信息
     * @param nodeId
     * @param processDefinitionKey
     * @return
     */
    public JSONArray nextTasksByNodeId(
                                String nodeId,
                                String processDefinitionKey
    ) {
        String map = feignClient.nextTasksByNodeId(nodeId, processDefinitionKey);
        JSONObject json = JSONUtil.parseObj(map);
        JSONObject content = json.getJSONObject("content");
        if (content != null) {
            JSONArray nodeList = content.getJSONArray("data");
            return nodeList;
        }
        return null;
    }

    @RequestMapping(
            value = {"/getFlowUserByNodeId"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    public JSONArray getFlowUserByNodeId(@RequestParam(required = false) String nodeId,
                                      @RequestParam(required = false) String processDefinitionKey,
                                      @RequestParam(required = false) String startUserId,
                                      @RequestParam(required = false) String assigneeId
    ){
        String flowUserByNodeId = feignClient.getFlowUserByNodeId(nodeId, processDefinitionKey, startUserId, assigneeId);
        JSONObject json = JSONUtil.parseObj(flowUserByNodeId);
        JSONObject content = json.getJSONObject("content");
        if (content != null) {
            JSONArray userList = content.getJSONArray("nodeDefinationUsers");
            return userList;
        }
        return null;
    }

    @RequestMapping(
            value = {"/startProcessByKeyNew"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    public Map<String, Object> startProcessByKeyNew(@RequestParam(required = true) String businessKey,
                                                    @RequestParam(required = false) String processDefinitionKey,
                                                    @RequestParam(required = false) String title,
                                                    @RequestParam(required = false) String moduleId,
                                                    @RequestParam(required = false) String firstNode,
                                                    @RequestParam(required = false) String reportStatus,
                                                    @RequestParam(required = false) String mijiid,
                                                    @RequestParam(required = false) String tableName,
                                                    @RequestParam(required = false) String comminfoId,
                                                    @RequestParam(required = false) String formType) {
        return feignClient.startProcessByKeyNew(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, formType);
    }

    @PostMapping("/updateReportStatus")
    public Boolean updateReportStatus(@RequestParam(required = true) String taskId,
                                      @RequestParam(required = true) String reportStatus){
        return feignClient.updateReportStatus(taskId,reportStatus);
    }

    /**
     * 流程提交接口
     */
    @RequestMapping(
            value = {"/taskCompleteNew"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    public Map<String, Object> taskCompleteNew(String businessKey,
                                               String currentTaskId,
                                               String nextTaskActivityId,
                                               String nextTaskUserIds,
                                               String nextTaskUsernames,
                                               String assigneeId,
                                               String moduleId,
                                               String nodeName,
                                               String reportStatus,
                                               String nodeCode,
                                               String rwlxs,
                                               String exField5,
                                               String processDefinitionId, // 流程监控传入piid
                                               String currentNodeId, // 流程监控传入的 nodeid
                                               String tjlx,   // 提交类型，判断不为空并且等于 lcjk 说明是流程监控提交的
                                               String logtype,//jump流程跳转
                                               String tableName,
                                               String comminfoId,
                                               String processDefinitionKey,
                                               String formType
    ){
        Map<String, Object> map = feignClient.taskCompleteNew(
                businessKey,
                currentTaskId,
                nextTaskActivityId,
                nextTaskUserIds,
                nextTaskUsernames,
                assigneeId,
                moduleId,
                nodeName,
                reportStatus,
                nodeCode,
                rwlxs,
                exField5,
                processDefinitionId,
                currentNodeId,
                tjlx,
                logtype,
                tableName,
                comminfoId,
                processDefinitionKey,
                formType
        );
        return map;
    }



}